package com.aspire.moss.portal.view;

import com.aspire.moss.portal.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.Layout.Alignment;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class IconView extends LinearLayout {
	
	Bitmap deleteIcon;
	
	 public IconView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a =  context.obtainStyledAttributes(attrs, R.styleable.IconView);
		deleteIcon = ((BitmapDrawable)a.getDrawable(R.styleable.IconView_deleteIcon)).getBitmap();
		a.recycle();
	}

	private static final int MAX_LINES = 2;
	AppInfo appInfo;
	
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
//		if(null != appInfo){
//			int mWidth = getMeasuredWidth();
//			if(null != appInfo.iconBitmap){
//				int left = (mWidth - appInfo.iconBitmap.getWidth())/ 2;
//				int top = 5;
//				canvas.drawBitmap(appInfo.iconBitmap , left , top , null);
//				canvas.drawBitmap(createTextBitmap(appInfo.title, mWidth), left, top + mWidth + 5, null);
//			}
//		}
		
		if (Workspace.isDynamic && null != deleteIcon){
			canvas.drawBitmap(deleteIcon, 0, 0, null);
			canvas.save();
		}
	}
	
	Bitmap createTextBitmap(String text , int mTextWidth) {
        Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredWidth(), Bitmap.Config.ALPHA_8);
        int density =  (int) getContext().getResources().getDisplayMetrics().density;
        b.setDensity(density);
        Canvas c = new Canvas(b);

        TextPaint mTextPaint = new TextPaint();
        StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
                Alignment.ALIGN_CENTER, 1, 0, true);
        int lineCount = layout.getLineCount();
        if (lineCount > MAX_LINES) {
            lineCount = MAX_LINES;
        }
        //if (!TEXT_BURN && lineCount > 0) {
            //RectF bubbleRect = mBubbleRect;
            //bubbleRect.bottom = height(lineCount);
            //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
        //}
        for (int i=0; i<lineCount; i++) {
            //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
            //int y = mFirstLineY + (i * mLineHeight);
            final String lineText = text.substring(layout.getLineStart(i), layout.getLineEnd(i));
//            int x = (int)(mBubbleRect.left
//                    + ((mBubbleRect.width() - mTextPaint.measureText(lineText)) * 0.5f));
//            int y = mFirstLineY + (i * mLineHeight);
            c.drawText(lineText, 0, 0, mTextPaint);
        }

        return b;
    }

}
